//    Copyright (C) Kherty.  All rights reserved.

using System;
using System.Windows;
using System.Windows.Forms;
using DevExpress.Xpf.Editors;
using OpenLS.Spreadsheet.UI.ViewModels;
using OpenLS.UI.Interfaces;
using QuantumWorkspace.SessionManager;
using Button = System.Windows.Controls.Button;

namespace OpenLS.Spreadsheet.UI
{
    /// <summary>
    /// Interaction logic for NewDefinedNameWindow.xaml
    /// </summary>

    public partial class LoginControl : IDialogResultControl
    {

        public LoginViewModel DefaultViewModel = new LoginViewModel();

        public LoginControl()
        {
            Resources.MergedDictionaries.Add(new DialogResources());
            InitializeComponent();
            this.DataContext = DefaultViewModel;
            this.Loaded += LoginControl_Loaded;
        }

        void LoginControl_Loaded(object sender, RoutedEventArgs e)
        {
            // In order to get the initial validation, we manually fire the bindings
            UserTextBox.GetBindingExpression(TextEdit.TextProperty).UpdateSource();
            PasswordTextBox.GetBindingExpression(TextEdit.TextProperty).UpdateSource();
        }

        
        private void OnOkClicked(object sender, RoutedEventArgs e)
        {
            // We test if the sender is the button to avoid the cases 
            // when the user click enter on a form textbox
            if (sender is Button)
            {
                ((Button)sender).Focus();
                if (OnOkButtonClicked != null)
                {
                    if (DefaultViewModel.DoLogin())
                        OnOkButtonClicked(this, new EventArgs());
                }
            }
        }

        private void OnCancelClicked(object sender, RoutedEventArgs e)
        {
            // We test if the sender is the button to avoid the cases 
            // when the user click enter on a form textbox
            if (sender is Button)
            {

                ((Button)sender).Focus();
                if (OnCancelButtonClicked != null)
                    OnCancelButtonClicked(this, new EventArgs());

            }
        }

        public event EventHandler OnOkButtonClicked;
        public event EventHandler OnCancelButtonClicked;
    }
}